<template>
    <div class="layout-container">
        <el-container>
            <el-header height="60px">
                <div class="header-container">
                    <div class="logo">
                        <h1>拍卖交易系统</h1>
                    </div>
                    <div class="nav-buttons">
                        <router-link to="/market">
                            <el-button type="primary" plain>前往拍卖市场</el-button>
                        </router-link>
                    </div>
                    <div class="user-info">
                        <el-dropdown trigger="click" @command="handleCommand">
                            <span class="user-dropdown">
                                {{ userName }}<el-icon class="el-icon--right"><arrow-down /></el-icon>
                            </span>
                            <template #dropdown>
                                <el-dropdown-menu>
                                    <el-dropdown-item command="profile">个人中心</el-dropdown-item>
                                    <el-dropdown-item command="logout">退出登录</el-dropdown-item>
                                </el-dropdown-menu>
                            </template>
                        </el-dropdown>
                    </div>
                </div>
            </el-header>

            <el-container>
                <el-aside width="200px">
                    <el-menu default-active="1" class="el-menu-vertical" background-color="#545c64" text-color="#fff"
                        active-text-color="#ffd04b" router>
                        <el-menu-item index="/user">
                            <el-icon>
                                <house />
                            </el-icon>
                            <span>用户中心</span>
                        </el-menu-item>

                        <el-sub-menu index="1">
                            <template #title>
                                <el-icon>
                                    <user />
                                </el-icon>
                                <span>我的账户</span>
                            </template>
                            <el-menu-item index="/user/profile">
                                <el-icon>
                                    <document />
                                </el-icon>
                                <span>个人信息</span>
                            </el-menu-item>
                            <el-menu-item index="/user/address">
                                <el-icon>
                                    <location />
                                </el-icon>
                                <span>收货地址</span>
                            </el-menu-item>
                        </el-sub-menu>

                        <el-sub-menu index="2">
                            <template #title>
                                <el-icon><shopping-cart /></el-icon>
                                <span>我的交易</span>
                            </template>
                            <el-menu-item index="/user/bids">
                                <el-icon>
                                    <ticket />
                                </el-icon>
                                <span>我的竞拍</span>
                            </el-menu-item>
                            <el-menu-item index="/user/orders">
                                <el-icon><shopping-bag /></el-icon>
                                <span>我的订单</span>
                            </el-menu-item>
                        </el-sub-menu>

                        <!-- 卖家功能 -->
                        <el-sub-menu index="3" v-if="userStore.isSeller">
                            <template #title>
                                <el-icon>
                                    <sell />
                                </el-icon>
                                <span>卖家中心</span>
                            </template>
                            <el-menu-item index="/user/seller">
                                <el-icon><data-analysis /></el-icon>
                                <span>卖家数据</span>
                            </el-menu-item>
                            <el-menu-item index="/user/seller/auction/publish">
                                <el-icon>
                                    <plus />
                                </el-icon>
                                <span>发布拍卖品</span>
                            </el-menu-item>
                            <el-menu-item index="/user/seller/auctions">
                                <el-icon>
                                    <goods />
                                </el-icon>
                                <span>我的拍卖品</span>
                            </el-menu-item>
                        </el-sub-menu>

                        <!-- 申请成为卖家 -->
                        <el-menu-item index="/user/seller/apply" v-if="!userStore.isSeller">
                            <el-icon>
                                <shop />
                            </el-icon>
                            <span>申请成为卖家</span>
                        </el-menu-item>
                    </el-menu>
                </el-aside>

                <el-main>
                    <router-view />
                </el-main>
            </el-container>
        </el-container>
    </div>
</template>

<script setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/user'
import { ElMessageBox } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()

const userName = computed(() => userStore.name || userStore.username)

const handleCommand = (command) => {
    if (command === 'logout') {
        ElMessageBox.confirm('确定要退出登录吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            userStore.logout()
            router.push('/login')
        }).catch(() => { })
    } else if (command === 'profile') {
        router.push('/user/profile')
    }
}
</script>

<style scoped>
.layout-container {
    height: 100%;
}

.el-container {
    height: 100%;
}

.el-header {
    background-color: #409EFF;
    color: #fff;
    line-height: 60px;
    padding: 0 20px;
}

.header-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.logo h1 {
    margin: 0;
    font-size: 20px;
}

.nav-buttons {
    display: flex;
    gap: 10px;
}

.user-dropdown {
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
}

.el-aside {
    background-color: #545c64;
}

.el-menu {
    border-right: none;
}

.el-menu-vertical:not(.el-menu--collapse) {
    width: 200px;
    min-height: 400px;
}
</style>